package org.sky.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysOrganType;
import org.sky.sys.entity.SysResource;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.mapper.SysOrganTypeMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weifx
 * @Title: SysOrganTypeService
 * @ProjectName springboot-platform
 * @Description: TODO{组织机构业务类}
 * @date 2019/3/7 20:51
 * @Version 1.0
 */
@Service
public class SysOrganTypeService {
    private final static Logger log = LogUtils.getLogger(SysFunService.class);
    @Autowired
    private SysOrganTypeMapper sysOrganTypeMapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;
    /**
     * 组织机构类型分页查询
     * @param pageNum
     * @param pageSize
     * @param example
     * @return
     */
    public PageListData selectSysOrganTypeByPage(int pageNum, int pageSize, Example example){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<SysOrganType> pageInfo = new PageInfo<SysOrganType>(sysOrganTypeMapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }

    /**
     * 保存{新增/修改}机构类型
     * @param sysOrganType
     * @throws ServiceException
     */
    public void saveAddEditSysOrganType(SysOrganType sysOrganType) throws ServiceException{
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null == sysOrganType.getId()) {
                Long id = SnowflakeIdWorker.getInstance().nextId();
                sysOrganType.setId(id);
                sysOrganType.setCreater(BspUtils.getLoginUser().getCode());
                sysOrganType.setCreateTime(ts);
                sysOrganType.setUpdater(BspUtils.getLoginUser().getCode());
                sysOrganType.setUpdateTime(ts);
                sysOrganTypeMapper.insertSelective(sysOrganType);
            } else {
                sysOrganType.setUpdater(BspUtils.getLoginUser().getCode());
                sysOrganType.setUpdateTime(ts);
                Example e = new Example(SysResource.class);
                e.createCriteria().andEqualTo("id", sysOrganType.getId());
                sysOrganTypeMapper.updateByExampleSelective(sysOrganType,e);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("组织机构类型保存失败:{}",e.getMessage());
            throw new ServiceException("组织机构类型保存失败:"+e.getMessage(),e);
        }
    }

    /**
     * 根据ID删除组织机构类型
     * @param list
     */
    public void delSysOrganTypeById(List<SysOrganType> list){
        try {
            List idList = new ArrayList();
            for (SysOrganType sysOrganType : list) {
                idList.add(sysOrganType.getId());
            }
            Example example = new Example(SysOrganType.class);
            example.createCriteria().andIn("id", idList);
            sysOrganTypeMapper.deleteByExample(example);
        }catch (Exception e){
            e.printStackTrace();
            log.error("组织机构类型删除失败:{}",e.getMessage());
            throw new ServiceException("组织机构类型删除失败:"+e.getMessage(),e);
        }
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public SysOrganType getSysOrganTypeById(Long id){
        Example example = new Example(SysOrganType.class);
        example.createCriteria().andEqualTo("id",id);
        return sysOrganTypeMapper.selectOneByExample(example);
    }
}
